// custom-tab-bar/index.js
const app = getApp();
const BaseURL = "http://cio.lvdengtech.com:9091/";
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {

    color: "#7A7E83",
    selectedColor: "#3B70FF",
    openType: "getUserInfo",
    list: [{
        pagePath: "/pages/PolicyService/index/index",
        text: "政策服务",
        iconPath: "/images/tarbar-icon1.png",
        selectedIconPath: "/images/tarbar-active-icon1.png",
        style: "width: 50rpx;height: 45rpx;",
      },
      {
        pagePath: "/pages/ConferenceService/index/index",
        text: "会议服务",
        iconPath: "/images/tarbar-icon2.png",
        selectedIconPath: "/images/tarbar-active-icon2.png",
        style: "width: 50rpx;height: 45rpx;",
      },
      {
        pagePath: "/pages/DemandCenter/index/index",
        text: "需求中心",
        iconPath: "/images/tarbar-icon3.png",
        selectedIconPath: "/images/tarbar-active-icon3.png",
        style: "width: 40rpx;height: 45rpx;",
      },
      {
        pagePath: "/pages/HrServices/index/index",
        text: "人才服务",
        iconPath: "/images/tarbar-icon4.png",
        selectedIconPath: "/images/tarbar-active-icon4.png",
        style: "width: 30rpx;height: 45rpx;",
      },
      {
        pagePath: "/pages/PlanServices/index/index",
        text: "方案服务",
        iconPath: "/images/tarbar-icon5.png",
        selectedIconPath: "/images/tarbar-active-icon5.png",
        style: "width: 30rpx;height: 45rpx;",
      },
    ],
  },

  attached() {
    let that = this;
    let token = wx.getStorageSync("token");
    let isToken = token?1:0
    if(isToken==1){
      this.setData({
        openType: " "
      })
    } else{
      this.setData({
        openType: "getUserInfo"
      })
    }
  },
  /**
   * 组件的方法列表
   */

  methods: {
    getUserInfo(e) {
      let that = this;
      let token = wx.getStorageSync("token");
      wx.showLoading({
        title: '请稍后',
      })
      if (token) {
       
        wx.hideLoading({
          complete: (res) => {
            that.switchTab(e);
          },
        })
      } else {
        if (e.detail.rawData) { //用户确认授权

          // let tempToken = wx.getStorageSync("tempToken");
          // let sessionKey = wx.getStorageSync("sessionKey");

          wx.login({
            success(res) {

              wx.request({
                url: BaseURL + "cio/miniapp/auth/wxlogin",
                method: "post",
                data: {
                  code: res.code,
                },
                header: {
                  'content-type': 'application/x-www-form-urlencoded' // 默认值
                },
                success(res1) {
                  if (res1.data.code == "200") {
                    wx.removeStorage({
                      key: 'token',
                    })
                    wx.setStorageSync("encryptedData", e.detail.encryptedData)
                    wx.setStorageSync("iv", e.detail.iv);
                    wx.setStorageSync("tempToken", res1.data.data.tempToken);
                    wx.setStorageSync("sessionKey", res1.data.data.sessionKey);
                    //在确定授权后再进行判断
                    wx.navigateTo({
                      url: '/pages/phoneToken/phoneToken',
                    })
                  }
                }
              })
            }

          })

          // wx.request({
          //   url: BaseURL + "cio/miniapp/auth/login",
          //   method: "post",
          //   header: {
          //     'content-type': 'application/x-www-form-urlencoded' // 默认值
          //   },
          //   data: {
          //     tempToken: tempToken,
          //     sessionKey: sessionKey,
          //     encryptedData: e.detail,
          //     iv: e.detail,
          //     phoneEncryptedData: e.detail,
          //     // phoneIv: e.detail,

          //   },
          //   success(res) {


          //     if (res.data.code == "200") {
          //       wx.setStorageSync("token", res.data.data.token);

          //       wx.setStorageSync("userInfo", res.data.data)

          //       console.log(wx.getStorageSync("userInfo"))
          //       that.switchTab(e);
          //     } else {
          //       wx.showToast({
          //         title: '登陆失败,请检查网络情况',
          //         icon: 'none',
          //         duration: 2000
          //       })
          //     }
          //   }
          // });

        }else{
                wx.showToast({
                  title: '请先授权再进行其他操作',
                  icon: 'none',
                  duration: 2000
                })
                

        }


      }
    },
    switchTab(e) {
      let token = wx.getStorageSync("token");
      let isToken = token?1:0
      if(isToken==1){
        const data = e.currentTarget.dataset;
        const url = data.path;

        wx.switchTab({
          url,
        });
        this.setData({
          selected: data.index,
        });
      }


    },
  },
});